﻿package com.spinettaro.arduinoledscontrol;


import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

	
	
	String TAG = "ArduinoLedsControl";
	String[] plan = new String[] {"L","F","W","U"};
	// set in an array the id of every switch
	//private final static Integer[] ids = { R.id.switch1 };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.activity_main);
		//registerLedChangeListener(ids);
		Log.d(TAG, "wtf");
		//sendPlan(plan);
	}
	
	
	
	public void sendPlan(String[] plan){
		for(int i=0; i<6; i++){
			sendString(plan[i]);
			try{
				Thread.sleep(5000);
			}
			catch(InterruptedException e){
				//Handle exception. 
			}
			
		}
		
		
	}
	private ArduinoReceiver myReceiver = new ArduinoReceiver(); //pass intent here?
	
	protected void onResume() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("primavera.arduino.intent.action.DATA_RECEIVED");
        registerReceiver(myReceiver, filter);
        super.onResume();
        Log.d(TAG,"RECEIVER REGISTERED");
    }

    @Override
    protected void onPause() {
        unregisterReceiver(myReceiver);
        super.onPause();
    }

	// register all onCheckedChangeListener for all Switch
	/*private void registerLedChangeListener(Integer... ids) {
		for (int i = 0; i < ids.length; i++) {
			Integer id = ids[i];
			Switch led = (Switch) findViewById(id);
			led.setOnCheckedChangeListener(this);
		}
		Log.d(TAG, "2");
	}*/

	
	
	//App never enters this method. 
	/*public void loop(View v) {

		try {
			// gone
			sendString("1");
			Thread.sleep(50);
			sendString("0");
			Thread.sleep(50);
			Log.d(TAG, "3");

		} catch (Exception e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
		}
	}*/

	
	//App never enters this method. 
	/*@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		Log.d(TAG, "4");
		return true;
		
	}*/


	
	
	
	
	/*@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		Log.d(TAG, "5");
		switch (buttonView.getId()) {
		case R.id.switch1:
			if (isChecked){
				
				sendString("1");
			
				Toast.makeText(getApplicationContext(), "LED is ON", Toast.LENGTH_LONG).show();
			
			}else
			{
				sendString("0");
				Toast.makeText(getApplicationContext(), "LED is OFF", Toast.LENGTH_LONG).show();
				Log.d(TAG, "TEST");
			}
			break;

		default:
			break;
		}
	}*/

	
	
	
	
	

	
	//This function sends a string of our choice to the Arduino Uno Communicator v1.33 app
	//via sendBroadcast(). When passed the Intent specifying the action "primavera.arduino.intent.action.SEND_DATA",
	//the appropriate intent filter in Arduino Uno Communicator v1.33 recognizes the intent, and routines in the Arduino Uno Communicator v1.33
	//app extract the byte array that represents the characters in the string attached to the sent intent. 
	private void sendString(String toSend) {
	
		    Log.d(TAG, toSend);
			Intent i = new Intent("primavera.arduino.intent.action.SEND_DATA");
			i.putExtra("primavera.arduino.intent.extra.DATA", toSend.getBytes());
			sendBroadcast(i);
			Log.d(TAG, "string sent");
		
		}

	}

